using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
namespace Acms.SqlDal
{
    public class UserPostCommentDal : Base
    {
        public enum Fields
        {
            Id,
		    UserId,
            UserName,
		    PostId,
		    Title,
		    Comment,
            IPAddress,
		    Reviewed,
		    Reported,
		    Suspended,
		    Created
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.UserId, reader, table);
            GetReaderValue<string>(Fields.UserName, reader, table);
            GetReaderValue<int>(Fields.PostId, reader, table);
            GetReaderValue<string>(Fields.Title, reader, table);
            GetReaderValue<string>(Fields.Comment, reader, table);
            GetReaderValue<System.Net.IPAddress>(Fields.IPAddress, reader, table);
            GetReaderValue<bool>(Fields.Reviewed, reader, table);
            GetReaderValue<bool>(Fields.Reported, reader, table);
            GetReaderValue<bool>(Fields.Suspended, reader, table);
            GetReaderValue<DateTime>(Fields.Created, reader, table);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return base.ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllByPostId(int postId)
        {
            SqlCommand cmd = CreateCommand("GetAllByPostId");
            AddInputParameter(cmd, "PostId", postId, SqlDbType.Int);
            return base.ExecuteGetCollection(cmd);
        }

        public void Update(ref int id, int userId, ref string userName, int postId, string title, string comment, System.Net.IPAddress ipAddress, bool reviewed, bool reported, bool suspended, DateTime created, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "PostId", postId, SqlDbType.Int);
            AddInputParameter(cmd, "Title", title, SqlDbType.VarChar, 500);
            AddInputParameter(cmd, "Comment", comment, SqlDbType.VarChar, 2000);
            AddInputParameter(cmd, "IPAddress", ipAddress, SqlDbType.VarChar, 16);
            AddInputParameter(cmd, "Reviewed", reviewed, SqlDbType.Bit);
            AddInputParameter(cmd, "Reported", reported, SqlDbType.Bit);
            AddInputParameter(cmd, "Suspended", suspended, SqlDbType.Bit);
            AddInputParameter(cmd, "Created", created, SqlDbType.DateTime);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "UserNameOut", SqlDbType.VarChar, 50);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            userName = GetOutputValue<string>(cmd, "UserNameOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");

        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }
    }
}
